/*
 * @Author: lhl 1079161148@qq.com
 * @Date: 2024-04-04 09:55:24
 * @LastEditors: lhl 1079161148@qq.com
 * @LastEditTime: 2024-04-06 15:51:43
 * @FilePath: \net-cloud-music\src\router\index.ts
 * @Description: 路由配置文件
 */
import { createRouter, createWebHistory } from 'vue-router'
// 方式一：tsconfig.json配置 "preserveValueImports": false
// 方式二：解决ts报错 Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/vue-router.js?v=e60463b7' does not provide an export named 'RouteRecordRaw' (at routes.ts:1:9)
import type { RouteRecordRaw } from 'vue-router'
// 首页一般不懒加载
import Index from '@/layout/Index.vue'

// 进度条
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// 修改进度条插件的配置
Nprogress.configure({
  showSpinner: false
})

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/index',
    component: Index,
    children: [
      {
        path: '/index',
        name: 'index',
        meta: {
          title: '推荐',
          i18nTitle: 'tabBarItem.home'
        },
        component: () => import('@/views/Index.vue')
      },
      {
        path: '/discover',
        name: 'discover',
        meta: {
          title: '发现',
          i18nTitle: 'tabBarItem.home'
        },
        component: () => import('@/views/discover/Index.vue')
      },
      {
        path: '/roam',
        name: 'roam',
        meta: {
          title: '漫游',
          i18nTitle: 'tabBarItem.home'
        },
        component: () => import('@/views/roam/Index.vue')
      },
      {
        path: '/dynamic',
        name: 'dynamic',
        meta: {
          title: '动态',
          i18nTitle: 'tabBarItem.home'
        },
        component: () => import('@/views/dynamic/Index.vue')
      },
      {
        path: '/my',
        name: 'my',
        meta: {
          title: '我的',
          i18nTitle: 'tabBarItem.home'
        },
        component: () => import('@/views/my/Index.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('@/views/login/Login.vue')
  }
]

// 路由
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 前置首位 访问权限控制
router.beforeEach((to, from, next) => {
  // 开启页面进度条
  Nprogress.start()
  next()
  // 用户仓库
  // const store = useUserStore()
  // 用户白名单
  const wihteList = ['/login']
  // 没有token 并且 不再白名单 则跳转登录页
  // if (!store.user?.token && !wihteList.includes(to.path)) return '/login'
  // 放行 return true  可以不用写
})

// 后置守卫
router.afterEach((to) => {
  // 设置页面标题
  document.title = (to.meta.title as string) || '奔跑的代码!' + Nprogress.done()
})

export default router
